package roborally.script.singleCondition;

import exception.IllegalPositionException;
import roborally.model.Element;
import roborally.model.Position;
import roborally.model.Robot;
import roborally.model.Wall;
import roborally.script.SingleCondition;
import be.kuleuven.cs.som.annotate.*;
import be.kuleuven.cs.som.taglet.*;

/**
 * @author Joren VERSPEURT
 * @author Mathieu REYMOND
 * @version 3.0
 * 
 * The class representing an AtWall statement in the RoboRally script language.
 */
public class AtWall extends SingleCondition {

	/**
	 * Create a new AtWall condition with a given Robot.
	 * @param robot The Robot this AtWall condition will apply to.
	 */
	public AtWall(Robot robot) {
		super(robot);
	}

	/**
	 * This Condition is true when <code>robot</code> is next to a Wall,
	 * provided that the Wall's Position is on the right of <code>robot</code>'s Position.
	 */
	@Override
	public boolean isTrue() {
		
		Position right;
		try {
			right = robot.getPosition().getNeighbour(robot.getOrientation().turn90Clockwise());
		} catch (IllegalPositionException e) {
			return false;
		}
		boolean result = false;
		for(Element element : robot.getBoard().getElements(right)) {
			if(element.getClass().isAssignableFrom(Wall.class)) {
				result = true;
				break;
			}
		}
		
		return result;
	}
	
	/**
	 * The script word for this condition.
	 */
	@Override
	public String toString() {
		return "wall";
	}
}
